package com.baseframe.common.aspect;


import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baseframe.common.annotation.DataFilter;
import com.baseframe.common.exception.RRException;
import com.baseframe.common.utils.Constant;
import com.baseframe.sys.entity.FrameUser;
import com.baseframe.sys.service.IFrameOuService;
import com.baseframe.sys.service.IFrameUserRoleService;
import com.baseframe.sys.shiro.ShiroUtils;

/**
 * 数据过滤，切面处理类
 *
 */
@Aspect
@Component
public class DataFilterAspect {
    @Autowired
    private IFrameOuService sysDeptService;
    @Autowired
    private IFrameUserRoleService userRoleService;
    @Autowired
    private IFrameUserRoleService sysUserRoleService;
    /*@Autowired
    private SysRoleDeptService sysRoleDeptService;*/

    @Pointcut("@annotation(com.baseframe.common.annotation.DataFilter)")
    public void dataFilterCut() {

    }

    @Before("dataFilterCut()")
    public void dataFilter(JoinPoint point) throws Throwable {
        Object params = point.getArgs()[0];
        if(params != null && params instanceof Map){
            FrameUser user = ShiroUtils.getUserEntity();

            //如果不是超级管理员，则进行数据过滤
            if (userRoleService.hasRight(user.getUserguid(),Constant.SUPER_ADMIN_ROLE)){
                Map map = (Map)params;
                map.put(Constant.SQL_FILTER, getSQLFilter(user, point));
            }

            return ;
        }

        throw new RRException("数据权限接口，只能是Map类型参数，且不能为NULL");
    }

    /**
     * 获取数据过滤的SQL
     */
    private String getSQLFilter(FrameUser user, JoinPoint point){
        MethodSignature signature = (MethodSignature) point.getSignature();
        DataFilter dataFilter = signature.getMethod().getAnnotation(DataFilter.class);
        //获取表的别名
        String tableAlias = dataFilter.tableAlias();
        if(StringUtils.isNotBlank(tableAlias)){
            tableAlias +=  ".";
        }

        //用户部门ID列表
        Set<String> deptIdList = new HashSet<>();
        deptIdList.add(user.getOuguid());

        //用户角色对应的部门ID列表
        //List<String> roleIdList = sysUserRoleService.queryRoleIdList(user.getUserguid());
        /*if(roleIdList.size() > 0){
            List<String> userDeptIdList = sysRoleDeptService.queryDeptIdList(roleIdList.toArray(new Long[roleIdList.size()]));
            deptIdList.addAll(userDeptIdList);
        }*/

        //用户子部门ID列表
        /*if(dataFilter.subDept()){
            List<Long> subDeptIdList = sysDeptService.getSubDeptIdList(user.getDeptId());
            deptIdList.addAll(subDeptIdList);
        }*/

        StringBuilder sqlFilter = new StringBuilder();
        sqlFilter.append(" (");
        sqlFilter.append(tableAlias).append("dept_id in(").append(StringUtils.join(deptIdList, ",")).append(")");

        //没有本部门数据权限，也能查询本人数据
        if(dataFilter.user()){
            sqlFilter.append(" or ").append(tableAlias).append("user_id=").append(user.getUserguid());
        }
        sqlFilter.append(")");

        return sqlFilter.toString();
    }
}
